package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.http.HttpUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.service.IChannelService;
import com.dkd.manage.service.INodeService;
import com.dkd.manage.service.IVmTypeService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * 设备管理Service业务层处理
 * 
 * @author 赵昊鹏
 * @date 2024-10-10
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private INodeService nodeService;
    @Autowired
    private IVmTypeService vmTypeService;
    @Autowired
    private IChannelService channelService;

    private static final Logger log = LoggerFactory.getLogger(VendingMachineServiceImpl.class);

    @Value("${baidu.map.geocoding-url}")
    private String geocodingUrl;

    /**
     * 从百度地图 API 获取地址的经纬度
     */
    private void setLatLngFromAddress(VendingMachine vendingMachine, String address) {
        try {
            String encodedAddress = URLEncoder.encode(address, StandardCharsets.UTF_8.toString());
            String url = String.format(geocodingUrl, encodedAddress);
            String response = HttpUtils.sendGet(url);

            // 使用正则表达式解析 JSON 响应
            Pattern statusPattern = Pattern.compile("\"status\":(\\d+)");
            Pattern latPattern = Pattern.compile("\"lat\":([\\d.]+)");
            Pattern lngPattern = Pattern.compile("\"lng\":([\\d.]+)");

            Matcher statusMatcher = statusPattern.matcher(response);
            Matcher latMatcher = latPattern.matcher(response);
            Matcher lngMatcher = lngPattern.matcher(response);

            if (statusMatcher.find() && statusMatcher.group(1).equals("0") && latMatcher.find() && lngMatcher.find()) {
                double lat = Double.parseDouble(latMatcher.group(1));
                double lng = Double.parseDouble(lngMatcher.group(1));

                vendingMachine.setLatitude(lat);
                vendingMachine.setLongitudes(lng);
            } else {
                log.error("无法获取这个地址的经纬度: " + address);
            }
        } catch (Exception e) {
            log.error("获取地址经纬度时发生错误:" + address, e);
        }
    }

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        //1.新增设备
        //1-1生成8位的唯一标识，补充货道编号
        String innerCode=UUIDUtils.getUUID();
        vendingMachine.setInnerCode(innerCode);
        //1-2 查询售货机类型表，补充设备容量
        VmType vmType = vmTypeService.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        //1-3 查询点位表，补充 区域、点位、合作商等信息
        Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
        BeanUtil.copyProperties(node, vendingMachine, "id");//拷贝除id外的其他属性
        vendingMachine.setAddr(node.getAddress());//拷贝地址
        setLatLngFromAddress(vendingMachine, node.getAddress());//获取经纬度
        //1-4 设备状态
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY);//0表示未投放
        vendingMachine.setCreateTime(DateUtils.getNowDate());//创建时间
        vendingMachine.setUpdateTime(DateUtils.getNowDate());//更新时间
        //1-5 保存
        int result =vendingMachineMapper.insertVendingMachine(vendingMachine);
        //2.新增货道
        //2-1 声明货道集合
        List<Channel> channelList = new ArrayList<>();
        //2-2 双层for循环
        for (int i = 1; i <= vmType.getVmRow(); i++) { // 外层行
            for (int j = 1; j <= vmType.getVmCol(); j++) {// 内层列
                //2-3 封装channel
                Channel channel = new Channel();
                channel.setChannelCode(i + "-" + j);// 货道编号
                channel.setVmId(vendingMachine.getId());// 售货机id
                channel.setInnerCode(vendingMachine.getInnerCode());// 售货机编号
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());// 货道最大容量
                channel.setCreateTime(DateUtils.getNowDate());// 创建时间
                channel.setUpdateTime(DateUtils.getNowDate());// 更新时间
                channelList.add(channel);
            }
        }
        //2-4 批量新增货道
        channelService.batchInsertChannels(channelList);
        return result;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        if(vendingMachine.getNodeId()!=null)//包含点位id时补充点位相关的冗余字段
        {
            Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
            BeanUtil.copyProperties(node, vendingMachine, "id");//拷贝除id外的其他属性
            vendingMachine.setAddr(node.getAddress());//拷贝地址
            setLatLngFromAddress(vendingMachine, node.getAddress());//获取经纬度
        }
        //查询点位表，补充：区域、点位、合作商等信息
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    /**
     * 根据内部编号查询设备信息
     * @param innerCode
     * @return
     */
    @Override
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {
        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }

    @Override
    public int countByVmTypeId(Long vmTypeId) {
        return vendingMachineMapper.countByVmTypeId(vmTypeId);
    }

    @Override
    public int updateVendingMachineStatusByInnerCode(String innerCode, Long vmStatus) {
        return vendingMachineMapper.updateVendingMachineStatusByInnerCode(innerCode, vmStatus, DateUtils.getNowDate());
    }

    @Override
    public int updateVendingMachineLastSupplyTimeByInnerCode(String innerCode) {
        return vendingMachineMapper.updateVendingMachineLastSupplyTimeByInnerCode(innerCode, DateUtils.getNowDate());
    }
}
